package wangjg.mda.tra;

import wangjg.mda.model.obj.AttributeName;
import wangjg.mda.model.obj.ModelDataType;
import wangjg.mda.model.obj.ModelField;
import wangjg.mda.model.obj.Model;

public class ViewUtil {
	public static final String INPUT_TEXTFIELD = "text";		//文本框
	public static final String INPUT_SWITCH = "switch";			//布尔型开关
	public static final String INPUT_DATE = "date";				//日期
	public static final String INPUT_HIDDEN = "hidden";			//隐藏字段
	public static final String INPUT_SELECT = "select";			//单列表

	public Integer getTextLength(ModelField field){
		if(field.getLength()!=null){
			return field.getLength();
		}
		if(ModelDataType.BYTE.equals(field.getType())){
			return 3;
		}
		if(ModelDataType.CHAR.equals(field.getType())){
			return 1;
		}
		if(ModelDataType.SHORT.equals(field.getType())){
			return 5;
		}
		if(ModelDataType.INTEGER.equals(field.getType())){
			return 10;
		}
		if(ModelDataType.FLOAT.equals(field.getType())){
			return 12;
		}
		if(ModelDataType.DOUBLE.equals(field.getType())){
			return 22;
		}
		if(ModelDataType.DATE.equals(field.getType())){
			return 23;
		}
		if(ModelDataType.TIMESTAMP.equals(field.getType())){
			return 23;
		}
		return null;
	}
	
	public String getInputType(ModelField field){
		if(field.getAttribute("inputType")!=null){
			return (String) field.getAttribute(AttributeName.INPUTTYPE);
		}
		if(ModelDataType.DATE.equals(field.getType())||ModelDataType.TIMESTAMP.equals(field.getType())){
			return "date";
		}
		if(Boolean.TRUE.equals(field.getId()) && Boolean.TRUE.equals(field.getAuto())){
			return "hidden";
		}
		if(field.getReference()!=null){
			return "select";
		}
		if(ModelDataType.STRING.equals(field.getType())){
			if(field.getLength()!=null && field.getLength()==1){
				return "switch";
			}else{
				return "text";
			}
		}
		if((field.getLabel()!=null && field.getLabel().startsWith("是否"))
				||(field.getName()!=null && field.getName().startsWith("if")))
		{
			return "switch";
		}
		return "text";
	}

	public String getLabel(ModelField field){
		if(field.getLabel()!=null){
			return field.getLabel();
		}else{
			return field.getName();
		}
	}

	public String getLabel(Model model){
		if(model.getLabel()!=null){
			return model.getLabel();
		}else{
			return model.getName();
		}
	}
}
